﻿// https://mp.weixin.qq.com/s?__biz=MzUxNjY5NTYxNA==&mid=2247489252&idx=2&sn=3f1019d1c62614321779d8b4ff8b7d88&scene=21#wechat_redirect
// https://leetcode-cn.com/problems/happy-number/

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>

using namespace std;

// struct ListNode {
//     int val;
//     ListNode *next;
//     ListNode() : val(0), next(nullptr) {}
//     ListNode(int x) : val(x), next(nullptr) {}
//     ListNode(int x, ListNode *next) : val(x), next(next) {}
// };

class Solution {
public:
    bool isHappy(int n) {
        unordered_set<int> num;
        int temp = n;

        while (true) {
            int sum = 0;
            // int unit = temp % 10;
            while (temp != 0) {
                int unit = temp % 10;
                sum += unit * unit;
                temp /= 10;
            }
            if (sum == 1) {
                break;
            }
            if (num.find(sum) == num.end()) {
                num.insert(sum);
            } else {
                return false;
            }
            temp = sum;
        }
        return true;
    }
};

int main() {
    Solution obj = Solution();
}